package com.huan.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huan.yygh.model.hosp.Department;
import com.huan.yygh.model.hosp.Hospital;
import com.huan.yygh.repository.DepartmentRepository;
import com.huan.yygh.service.DepartmentServcie;
import com.huan.yygh.vo.hosp.DepartmentQueryVo;
import com.huan.yygh.vo.hosp.DepartmentVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DepartmentServiceImpl implements DepartmentServcie {
    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室
    @Override
    public void save(Map<String, Object> paramMap) {
        String mapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapString, Department.class);

        Department departmentExist =
                departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(departmentExist != null){
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //设置分页
        Pageable pageable = PageRequest.of(page-1,limit);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        //设置模糊查询
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);
        Page<Department> pages = departmentRepository.findAll(example, pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        /*
        * 最终封装的信息要是
        * 一个大科室下面有属于他科室的小科室列表
        *
            @ApiModelProperty(value = "科室编号")
            private String depcode;

            @ApiModelProperty(value = "科室名称")
            private String depname;

            @ApiModelProperty(value = "下级节点")
            private List<DepartmentVo> children;
	* */
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> collect =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 deparmentMap
        for(Map.Entry<String, List<Department>> entry : collect.entrySet()){
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> bigDeparmentList = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
                //大科室编号
            departmentVo.setDepcode(bigcode);
                //大科室名字
            departmentVo.setDepname(bigDeparmentList.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department department : bigDeparmentList){
                DepartmentVo dvDemo = new DepartmentVo();
                //获取小科室的编号并封装到DepartmentVO
                dvDemo.setDepcode(department.getDepcode());
                dvDemo.setDepname(department.getDepname());
                //将小科室的信息封装到小科室列表中
                children.add(dvDemo);
            }
            //把小科室list集合放到大科室children里面
            departmentVo.setChildren(children);
            //放到最终result里面
            result.add(departmentVo);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department =
                departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department =
                departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;

    }
}
